package com.cqupt.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cqupt.model.pojo.TimeRecord;
import com.cqupt.service.TimeRecordService;
import com.cqupt.mapper.TimeRecordMapper;
import com.cqupt.utils.AuthContextUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;


@Service
public class TimeRecordServiceImpl extends ServiceImpl<TimeRecordMapper, TimeRecord>
        implements TimeRecordService {

    @Resource
    private TimeRecordMapper timeRecordMapper;

    @Override
    public void updateTime(String date, int minutes) {
        Integer userId = AuthContextUtil.get().getId();
        LambdaQueryWrapper<TimeRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TimeRecord::getUserId, userId);
        wrapper.eq(TimeRecord::getDate, date);
        TimeRecord timeRecord = timeRecordMapper.selectOne(wrapper);
        if (ObjectUtil.isNotNull(timeRecord)) {
            timeRecord.setTime(timeRecord.getTime() + minutes);
            timeRecordMapper.updateById(timeRecord);
        } else {
            TimeRecord timeRecord1 = new TimeRecord();
            timeRecord1.setUserId(userId);
            timeRecord1.setDate(date);
            timeRecord1.setTime(minutes);
            timeRecordMapper.insert(timeRecord1);
        }
    }
}




